package cn.wolfcode.wolf2w.gateway.filter;

import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.gateway.util.Md5Util;
import com.alibaba.fastjson.JSON;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 浪兎兎
 * @since 2025-06-18 13:53
 */
@Component
public class SignFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        Map<String, Object> map = new HashMap<>();
        for (String key : queryParams.keySet()) {
            if("sign".equals(key)){
                continue;
            }
            List<String> list = queryParams.get(key);
            StringBuffer sb = new StringBuffer();
            for (String s : list) {
                sb.append(s);
            }
            String value = sb.toString();
            map.put(key, value);
        }
        String sign_server = Md5Util.signatures(map);
        String sign_client = queryParams.getFirst("sign");

        //因为当前项目只有info.vue中使用了该测试方案，其他都没有使用，其他请求都不会携带sign_client；
        //所以，当没有传递sign_client时，直接放行
        //当前端代码每个请求都携带sign_client时，将这段代码删除
        if(sign_client == null){
            return chain.filter(exchange);
        }


        if(!sign_client.equals(sign_server)){
            //参数被篡改
            HttpHeaders headers = response.getHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            R r = R.fail(500, "非法请求，参数被篡改");
            String jsonString = JSON.toJSONString(r);
            DataBuffer dataBuffer = response.bufferFactory().wrap(jsonString.getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Mono.just(dataBuffer));
        }

        String timestamp = queryParams.getFirst("timestamp");
        Long reqTime = Long.valueOf(timestamp);
        Long curTime = System.currentTimeMillis();
        if(curTime-reqTime > 20*1000){
            HttpHeaders headers = response.getHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            R r = R.fail(500, "接口访问超时");
            String jsonString = JSON.toJSONString(r);
            DataBuffer dataBuffer = response.bufferFactory().wrap(jsonString.getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Mono.just(dataBuffer));
        }


        return chain.filter(exchange);

    }
}